package com.daxia.ogmall.web.controller.admin;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.daxia.ogmall.dto.FactoryInfoDTO;
import com.daxia.ogmall.dto.JsonResultDTO;
import com.daxia.ogmall.service.FactoryInfoService;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.web.controller.BaseController;

@Controller
@RequestMapping(value = "/admin/factoryInfo", produces="text/html;charset=UTF-8")
public class AdminFactoryInfoController extends BaseController {

	@Autowired
	private FactoryInfoService factoryInfoService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id) {
		if (id != null) {
			FactoryInfoDTO dto = factoryInfoService.load(id);
			map.put("n", dto);
		}
		return "admin/factoryInfo/factoryInfo_detail";
	}
	
	@ResponseBody
	@RequestMapping(value = "/save")
	
	public String save(FactoryInfoDTO dto, JsonResultDTO resultDTO) throws Exception {
		if (dto.getId() == null) {
			factoryInfoService.create(dto);
		} else {
			factoryInfoService.updateAllFields(dto);
		}
		return okAndClose(resultDTO);
	}

    
	@ResponseBody
	@RequestMapping(value = "/delete") 
	public String delete(Long[] ids, JsonResultDTO resultDTO) throws IOException {
		factoryInfoService.deleteByIds(ids);
		return okAndRefresh(resultDTO);
	}
	
	@RequestMapping(value = "/list")
	
	public String list(FactoryInfoDTO dto, Map<String, Object> map, Page page) {
		List<FactoryInfoDTO> dtos = factoryInfoService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		return "admin/factoryInfo/factoryInfo_list";
	}
	
	@ResponseBody
	@RequestMapping(value = "/datagrid")
    
    public String datagrid(FactoryInfoDTO dto, Map<String, Object> map, Page page) throws Exception {
        List<FactoryInfoDTO> dtos = factoryInfoService.find(dto, page);
        JSONArray jsonArray = new JSONArray();
        for (FactoryInfoDTO factoryInfo : dtos) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", factoryInfo.getId());
	        jsonObject.put("user.nickName", factoryInfo.getUser().getNickName());    	
            jsonObject.put("email", factoryInfo.getEmail());
            jsonObject.put("legalPerson", factoryInfo.getLegalPerson());
            jsonObject.put("officeAddress", factoryInfo.getOfficeAddress());
            jsonObject.put("registerAddress", factoryInfo.getRegisterAddress());
            jsonObject.put("saleType", factoryInfo.getSaleType());
            jsonObject.put("website", factoryInfo.getWebsite());
            jsonObject.put("companySize", factoryInfo.getCompanySize());
            jsonObject.put("industry", factoryInfo.getIndustry());
            jsonObject.put("nature", factoryInfo.getNature());
            jsonObject.put("images", factoryInfo.getImages());
            jsonObject.put("remarks", factoryInfo.getRemarks());
            jsonObject.put("status", factoryInfo.getStatus());
            jsonObject.put("auditUser", factoryInfo.getAuditUser());
			jsonObject.put("auditTime", formatDate(factoryInfo.getAuditTime(), "yyyy-MM-dd HH:mm:ss"));	            	
            jsonObject.put("auditRemark", factoryInfo.getAuditRemark());
			jsonObject.put("createTime", formatDate(factoryInfo.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));	            	
            jsonArray.add(jsonObject);
        }
        JSONObject json = new JSONObject();
        json.put("total", page.getTotalRecords());
        json.put("rows", jsonArray);
        return json.toJSONString();
    }
}
